import React, { Component } from 'react';
import { connect } from 'dva';
import { message, Modal, Pagination, Popconfirm, Button } from 'antd';
import styles from './../Users.css';
import moment from 'moment';
import Factory from '../../factory';
import * as datetime from '../../../utils/datetime'
import dataEditCreate from '../../addons/DataEdit'
import dataTableCreate from '../../addons/DataTable'
import dataImportCreate from '../../addons/DataImport'
import * as menuService from '../../../services/menus';
const daoName = "CompTyHQ";
const ButtonGroup = Button.Group;
const columns = [
    { title: '导入结果', width: 250, sorter: true, dataIndex: 'CWXX', key: 'CWXX', dispaly: true, filter: true, export: false },
    { title: '日期', width: 110, sorter: true, key: 'DTID', render: data => datetime.format(data.DTID, "yyyy-MM-dd"), dispaly: true, filter: true, export: false },
    { title: '机构号', width: 110, sorter: true, dataIndex: 'JGH', key: 'JGH', dispaly: true, filter: true, export: true },
    { title: '账号', width: 220, sorter: true, dataIndex: 'ACNO', key: 'ACNO', dispaly: true, filter: true, export: true },
    { title: '户名', width: 300, sorter: true, dataIndex: 'CUNM', key: 'CUNM', dispaly: true, filter: true, export: true },
    { title: '余额', width: 120, sorter: true, dataIndex: 'ACBL', key: 'ACBL', dispaly: true, filter: true, export: true },
    { title: '利率', width: 100, sorter: true, dataIndex: 'INRT', key: 'INRT', dispaly: true, filter: true, export: true },
    { title: '类定期基数', width: 130, sorter: true, dataIndex: 'LDQJS', key: 'LDQJS', dispaly: true, filter: true, export: true },
    { title: '12月底积数', width: 130, sorter: true, dataIndex: 'YDJS12', key: 'YDJS12', dispaly: true, filter: true, export: true },
    { title: '1月底积数', width: 120, sorter: true, dataIndex: 'YDJS1', key: 'YDJS1', dispaly: true, filter: true, export: true },
    { title: '1月实际积数', width: 140, sorter: true, dataIndex: 'YSJJS1', key: 'YSJJS1', dispaly: true, filter: true, export: true },
    { title: '利润计算', width: 120, sorter: true, dataIndex: 'JSLR', key: 'JSLR', dispaly: true, filter: true, export: true },
    { title: '利润贡献', width: 120, sorter: true, dataIndex: 'LLGX', key: 'LLGX', dispaly: true, filter: true, export: true },
    { title: '1月分配利润数', width: 150, sorter: true, dataIndex: 'FPLRS1', key: 'FPLRS1', dispaly: true, filter: true, export: true },
    { title: '绩费', width: 120, sorter: true, dataIndex: 'JF', key: 'JF', dispaly: true, filter: true, export: true },
    { title: '工号1', width: 100, sorter: true, dataIndex: "GH1", key: 'GH1', dispaly: true, filter: true, export: true },
    { title: '客户经理1', width: 130, sorter: true, dataIndex: "KHJL1", key: 'KHJL1', dispaly: true, filter: true, export: true },
    { title: '占比1', width: 80, sorter: true, dataIndex: "ZB1", key: 'ZB1', dispaly: true, filter: false, export: true },
    { title: '工号2', width: 100, sorter: true, dataIndex: "GH2", key: 'GH2', dispaly: true, filter: true, export: true },
    { title: '客户经理2', width: 130, sorter: true, dataIndex: "KHJL2", key: 'KHJL2', dispaly: true, filter: true, export: true },
    { title: '占比2', width: 80, sorter: true, dataIndex: "ZB2", key: 'ZB2', dispaly: true, filter: false, export: true },
    { title: '工号3', width: 100, sorter: true, dataIndex: "GH3", key: 'GH3', dispaly: true, filter: true, export: true },
    { title: '客户经理3', width: 130, sorter: true, dataIndex: "KHJL3", key: 'KHJL3', dispaly: true, filter: true, export: true },
    { title: '占比3', width: 80, sorter: true, dataIndex: "ZB3", key: 'ZB3', dispaly: true, filter: false, export: true },
    { title: '工号4', width: 100, sorter: true, dataIndex: "GH4", key: 'GH4', dispaly: true, filter: true, export: true },
    { title: '客户经理4', width: 130, sorter: true, dataIndex: "KHJL4", key: 'KHJL4', dispaly: true, filter: true, export: true },
    { title: '占比4', width: 80, sorter: true, dataIndex: "ZB4", key: 'ZB4', dispaly: true, filter: false, export: true },
    { title: '工号5', width: 100, sorter: true, dataIndex: "GH5", key: 'GH5', dispaly: true, filter: true, export: true },
    { title: '客户经理5', width: 130, sorter: true, dataIndex: "KHJL5", key: 'KHJL5', dispaly: true, filter: true, export: true },
    { title: '占比5', width: 80, sorter: true, dataIndex: "ZB5", key: 'ZB5', dispaly: true, filter: false, export: true },
];
const controls = [
    { name: 'DTID', dataType: "date", type: "date", label: '日期', grid: 12 / 24 },
    { name: 'JGH', type: 'text', label: '机构号', grid: 12 / 24 },
    { name: 'ACNO', type: 'text', label: '账号', grid: 12 / 24 },
    { name: 'CUNM', type: 'text', label: '户名', grid: 12 / 24 },
    { name: 'ACBL', type: "number", dataType: "number", label: '余额', grid: 12 / 24 },
    { name: 'INRT', type: "number", dataType: "number", label: '利率', grid: 12 / 24 },
    { name: 'LDQJS', type: "number", dataType: "number", label: '类定期基数', grid: 12 / 24 },
    { name: 'YDJS12', type: "number", dataType: "number", label: '12月底积数', grid: 12 / 24 },
    { name: 'YDJS1', type: "number", dataType: "number", label: '1月底积数', grid: 12 / 24 },
    { name: 'YSJJS1', type: "number", dataType: "number", label: '1月实际积数', grid: 12 / 24 },
    { name: 'JSLR', type: "number", dataType: "number", label: '利润计算', grid: 12 / 24 },
    { name: 'LLGX', type: "number", dataType: "number", label: '利润贡献', grid: 12 / 24 },
    { name: 'FPLRS1', type: "number", dataType: "number", label: '1月分配利润数', grid: 12 / 24 },
    { name: 'JF', type: "number", dataType: "number", label: '绩费', grid: 12 / 24 },
    { name: 'GH1', type: 'text', label: '工号1', grid: 12 / 24 },
    { name: 'KHJL1', type: 'text', label: '客户经理1', grid: 12 / 24 },
    { name: 'ZB1', type: "number", dataType: "number", label: '占比1', grid: 12 / 24 },
    { name: 'GH2', type: 'text', label: '工号2', grid: 12 / 24 },
    { name: 'KHJL2', type: 'text', label: '客户经理2', grid: 12 / 24 },
    { name: 'ZB2', type: "number", dataType: "number", label: '占比2', grid: 12 / 24 },
    { name: 'GH3', type: 'text', label: '工号3', grid: 12 / 24 },
    { name: 'KHJL3', type: 'text', label: '客户经理3', grid: 12 / 24 },
    { name: 'ZB3', type: "number", dataType: "number", label: '占比3', grid: 12 / 24 },
    { name: 'GH4', type: 'text', label: '工号4', grid: 12 / 24 },
    { name: 'KHJL4', type: 'text', label: '客户经理4', grid: 12 / 24 },
    { name: 'ZB4', type: "number", dataType: "number", label: '占比4', grid: 12 / 24 },
    { name: 'GH5', type: 'text', label: '工号5', grid: 12 / 24 },
    { name: 'KHJL5', type: 'text', label: '客户经理5', grid: 12 / 24 },
    { name: 'ZB5', type: "number", dataType: "number", label: '占比5', grid: 12 / 24 },
    { name: 'CWXX', type: 'text', label: '处理结果', ignore: true, grid: 12 / 24 },
];
const EditFrom = dataEditCreate(daoName);
const DataTable = dataTableCreate(daoName);
const DataImport = dataImportCreate(daoName);
class TYHQDRForm extends Component {
    constructor(props) {
        super(props);
        this.state = {
            dataChange: false
        };
    }
    componentDidMount() {
        this.props.dispatch({
            type: `${daoName}/setKeyName`,
            payload: { keyName: "ID", permID: this.props.permID }
        });
        let sort = {
            field: 'DTID',
            order: 'descend'
        }
        this.props.dispatch({
            type: `${daoName}/fetch`,
            payload: { daoName, page: 1, pageSize: 20, sorter: sort }
        });
        this.loadOptionData();
    }
    componentWillReceiveProps(nextProps) {
        const { showImport } = this.props
        if (!nextProps.showImport && showImport) {
            this.props.dispatch({
                type: `${daoName}/fetch`,
                payload: { daoName, page: 1, pageSize: 20 }
            });
        }
    }
    loadOptionData() {
        let options = []
        controls.map(c => {
            if (c.fetch) {
                let f = c.fetch;
                if (c.type === 'cascader-select') {
                    f.isCascader = true;
                }
                if (c.type !== 'search') {
                    options.push(f);
                }
            }
        })
        if (daoName && options.length > 0) {
            this.props.dispatch({ type: `${daoName}/fetchOptions`, payload: { options } });
        }
    }
    showImportHandler() {
        this.props.dispatch({
            type: `${daoName}_DataImport/setDisplay`,
            payload: { display: true },
        });
    }
    DownTmportHandler() {
        let columnHeaders = []
        columns.map((c, j) => {
            if (!c.export) {
                return
            }
            let header = {
                Name: c.key,
                Caption: c.title,
                Width: c.width
            }
            columnHeaders.push(header)
        })
        let form = document.createElement('form');//定义一个form表单
        let whereItems = [];
        whereItems.push({
            FieldName: "1",
            Operator: "<>",
            FieldValues: "1"
        })
        const whereCauses = []
        whereCauses.push({
            WhereItems: whereItems,
            RelationSign: "AND"
        })
        const orderItems = []
        form.style = "display:none";
        form.target = "_blank";
        form.method = "post";
        form.action = "/api/ExportXlsx/" + daoName;
        let input1 = document.createElement("input");
        input1.type = "hidden";
        input1.name = "WhereCauses";
        input1.value = JSON.stringify(whereCauses);
        let input2 = document.createElement("input");
        input2.type = "hidden";
        input2.name = "OrderItems";
        input2.value = JSON.stringify(orderItems)
        let input3 = document.createElement("input");
        input3.type = "hidden";
        input3.name = "ColumnHeaders";
        input3.value = JSON.stringify(columnHeaders);
        let input4 = document.createElement("input");
        input4.type = "hidden";
        input4.name = "PermID";
        input4.value = "" + this.props.permID;
        let input5 = document.createElement("input");
        input5.type = "hidden";
        input5.name = "UserID";
        input5.value = "" + this.props.userID;
        document.body.appendChild(form);//将表单放置在web中
        form.appendChild(input1);
        form.appendChild(input2);
        form.appendChild(input3);
        form.appendChild(input4);
        form.appendChild(input5);
        form.submit();
        document.body.removeChild(form)
    }
    render() {
        let { showEdit, record, showImport, permID, popedomTypes } = this.props
        const pTypes = popedomTypes[permID]
        let data = null
        if (pTypes.indexOf("2") !== -1) {
            data = <ButtonGroup style={{ zIndex: 0 }}>
                <Button onClick={this.showImportHandler.bind(this)}>导入</Button>
                <Button onClick={this.DownTmportHandler.bind(this)}>下载导入模板</Button>
            </ButtonGroup>
        }
        return (
            <div className={styles.normal} style={{ background: '#fff', marginTop: -16, padding: '5px 5px' }}>
                <div>
                    <div className={styles.create}>
                        {data}
                    </div>
                    <DataTable ref="JjzbTable" daoName={daoName} columns={columns} controls={controls} permID={permID} />
                    <DataImport title="同业台账活期" importName="CompTyHQ" daoName={daoName} keys={["ID"]} paramNames={["DTID"]} importMode="DeleteAfterInsert" controls={controls} />
                </div>
            </div>
        )
    }
}
 
function mapStateToProps(state) { 
    const { showEdit, record, selectedRowKeys, keyName } = state[daoName];
    const { userID, orgID, pOrgID, popedomTypes } = state.menus;
    const { display } = state[`${daoName}_DataImport`];
    return {
        userID,
        orgID,
        pOrgID,
        popedomTypes,
        showEdit,
        selectedRowKeys,
        keyName,
        record,
        showImport: display
    }
}
TYHQDRForm = connect(mapStateToProps)(TYHQDRForm)
export default TYHQDRForm;

let render = (props) => React.createElement(TYHQDRForm, props);
let f = new Factory()
f.set("TYHQDRForm", render)
